package com.itheima.config;

import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

import javax.sql.DataSource;

/**
 * 包名:com.itheima.config
 * @author Leevi
 * 日期2020-08-11  11:42
 *    1. 将DriverManagerDataSource类进行IOC
 *        @Bean
 *        public DataSource getDataSource(){
 *            DriverManagerDataSource dataSource = new DriverManagerDataSource();
 *            dataSource.setUsername("root");
 *            dataSource.setPassword("123");
 *            dataSource.setUrl("jdbc:mysql:///day20?characterEncoding=utf8");
 *            dataSource.setDriverClassName("com.mysql.jdbc.Driver");
 *            return dataSource;
 *        }
 *    2. 将SqlSessionFactoryBean类进行IOC
 *       @Bean
 *       public SqlSessionFactoryBean getSqlSessionFactoryBean(DataSource dataSource){
 *           SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
 *           //设置dataSource属性
 *           sqlSessionFactoryBean.setDataSource(dataSource);
 *           //设置typeAliasesPackage
 *           sqlSessionFactoryBean.setTypeAliasesPackage("com.itheima.pojo");
 *           return sqlSessionFactoryBean;
 *       }
 *
 *    3. 将MapperScannerConfigurer类进行IOC
 *       @Bean
 *       public MapperScannerConfigurer getMapperScannerConfigurer(){
 *           MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
 *           mapperScannerConfigurer.setBasePackage("com.itheima.dao");
 *           return mapperScannerConfigurer;
 *       }
 *
 *    4. 使用PropertySource注解引入外部的properties文件
 */
@PropertySource(value = {"classpath:mybatis.properties"})
@Configuration
public class MybatisConfig {
    private String driver = "com.mysql.jdbc.Driver";
    private String url = "jdbc:mysql://192.168.47.129:3306/mybatis_day?characterEncoding=utf8";
    private String username = "root";
    private String password = "123456";
//    @Value("${jdbc.password}")
//    private String password;

    @Bean
    public DataSource getDataSource(){
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        dataSource.setUrl(url);
        dataSource.setDriverClassName(driver);
        return dataSource;
    }

    @Bean
    public SqlSessionFactoryBean getSqlSessionFactoryBean(DataSource dataSource){
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        //设置dataSource属性
        sqlSessionFactoryBean.setDataSource(dataSource);
        //设置typeAliasesPackage
        sqlSessionFactoryBean.setTypeAliasesPackage("com.itheima.pojo");
        return sqlSessionFactoryBean;
    }

    @Bean
    public MapperScannerConfigurer getMapperScannerConfigurer(){
        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
        mapperScannerConfigurer.setBasePackage("com.itheima.dao");
        return mapperScannerConfigurer;
    }
}
